package HouseRent.service;

import HouseRent.domain.House;

/**
 * @Package: houserent.service
 * @ClassName: HouseService
 * @Author: x2606
 * @CreateTime: 2022/3/4 18:46
 * @Description: 响应HouseView的调用
 */

public class HouseService {
    private House[] houses;//用于存储房屋对象
    private int houseNums=1;//记录当前有多少个房屋
    private int idCounter=1;//记录id增长到哪个值

    public HouseService(int size){
        houses = new House[size];//创建HouseServise对象时初始化房屋数组

        houses[0]=new House(1,"jack","123","北京",2000,"未出租");
    }

    //返回Houses数组
    public House[] list(){
        return houses;
    }

    //添加房屋
    public boolean add(House newHouse){
        //判断数组是否已满
        if (houseNums==houses.length) {
            System.out.println("数组已满");
            return false;
        }
        houses[houseNums++]=newHouse;
        newHouse.setId(++idCounter);
        return true;
    }

    //删除房屋
    public  boolean del(int delId){
        int index=-1;
        for (int i=0;i<houseNums;i++){
            if (houses[i].getId()==delId){
                index=i;
                break;
            }
        }
        if (index==-1){
            return false;
        }

        for (int i = index; i < houseNums-1; i++) {
            houses[i]=houses[i+1];
        }
        houses[--houseNums]=null;
        return true;
    }

    //查找房屋
    public House find(int findId){
        for (int i = 0; i < houseNums; i++) {
            if (houses[i].getId()==findId){
                return houses[i];
            }
        }
        return null;
    }
}
